// @algorithm @lc id=560 lang=cpp 
// @title subarray-sum-equals-k


// @test([1,1,1],2)=2
// @test([1,2,3],3)=2
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int,int> sumCnt;
        sumCnt[0] = 1;
        int preSum = 0, ans = 0;
        for(auto & i : nums){
            preSum += i;
            if(sumCnt.count(preSum-k))
                ans += sumCnt[preSum-k];
            sumCnt[preSum]++;
        }
        return ans;
    }
};